package com.share.device.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.constant.DeviceConstants;
import com.share.common.core.constant.SecurityConstants;
import com.share.common.core.domain.R;
import com.share.common.core.web.domain.AjaxResult;
import com.share.device.domain.Cabinet;
import com.share.device.domain.Station;
import com.share.device.domain.StationLocation;
import com.share.device.mapper.StationMapper;
import com.share.device.repository.StationLocationRepository;
import com.share.device.service.ICabinetService;
import com.share.device.service.IRegionService;
import com.share.device.service.IStationService;
import com.share.rule.api.RemoteFeeRuleService;
import com.share.rule.domain.FeeRule;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station>
        implements IStationService {

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private ICabinetService cabinetService;

    @Autowired
    private IRegionService regionService;

    @Autowired
    private StationLocationRepository stationLocationRepository;

    @Autowired
    private RemoteFeeRuleService feeRuleService;
    // 分页查询
    @Override
    public List<Station> selectStationList(Station station) {
        List<Station> list = stationMapper.selectStationList(station);
        for (Station sta : list) {
            Long cabinetId = sta.getCabinetId();
            String cabinetNo = null;
            if (cabinetId != null){
                Cabinet cabinet = cabinetService.getById(cabinetId);
                cabinetNo  = cabinet.getCabinetNo();
            }


            // 获取站点的规则id
            Long feeRuleId = sta.getFeeRuleId();
            if(feeRuleId != null){
                // 调用 Feign Client 获取规则信息
                R<FeeRule> feeRuleInfo = feeRuleService.getFeeRule(feeRuleId);
                    // 假设返回的data是FeeRule对象
                FeeRule feeRule = feeRuleInfo.getData();
                    sta.setFeeRuleName(feeRule.getName()); // 按需设置
            }

            sta.setCabinetNo(cabinetNo);
        }
        return list;
    }

    //添加
    @Override
    public int saveStation(Station station) {
        String provinceName = regionService.getNameByCode(station.getProvinceCode());
        String cityName = regionService.getNameByCode(station.getCityCode());
        String districtName = regionService.getNameByCode(station.getDistrictCode());
        station.setFullAddress(provinceName + cityName + districtName + station.getAddress());
        int rows = stationMapper.insert(station);

        StationLocation stationLocation = new StationLocation();
        stationLocation.setId(ObjectId.get().toString());
        stationLocation.setStationId(station.getId());
        stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
        stationLocation.setCreateTime(new Date());
        stationLocationRepository.save(stationLocation);

        return rows;
    }

    //修改
    @Override
    public int updateStation(Station station) {
        String provinceName = regionService.getNameByCode(station.getProvinceCode());
        String cityName = regionService.getNameByCode(station.getCityCode());
        String districtName = regionService.getNameByCode(station.getDistrictCode());
        station.setFullAddress(provinceName + cityName + districtName + station.getAddress());
        int rows = stationMapper.updateById(station);

        StationLocation stationLocation = stationLocationRepository.getByStationId(station.getId());
        if (stationLocation != null){
            stationLocation.setLocation(new GeoJsonPoint(station.getLongitude().doubleValue(), station.getLatitude().doubleValue()));
            stationLocationRepository.save(stationLocation);
        }
        return rows;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int setData(Station station) {
        this.updateById(station);

        //更正柜机使用状态
        Cabinet cabinet = cabinetService.getById(station.getCabinetId());
        cabinet.setStatus("1");
        cabinetService.updateById(cabinet);
        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeByIds(Collection<?> list) {
        for (Object id : list) {
            stationLocationRepository.deleteByStationId(Long.parseLong(id.toString()));
        }
        return super.removeByIds(list);
    }
}
